package edu.suda.xsap.dto.convert;

import edu.suda.xsap.dto.EmployeeDto;
import edu.suda.xsap.entity.Employee;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;

import java.util.List;

@Mapper(componentModel = "spring")
public interface EmployeeConvert {
    @Mapping(source = "id", target = "id")
    @Mapping(source = "name", target = "employeeName")
    @Mapping(source = "phone", target = "employeePhone")
    @Mapping(source = "sex", target = "employeeSex")
    @Mapping(source = "birthday", target = "employeeBirthday")
    @Mapping(source = "introduce", target = "employeeIntroduction")
    @Mapping(source = "avatarUrl", target = "employeeAvatarUrl")
    @Mapping(source = "note", target = "employeeNote")
    @Mapping(source = "roleName", target = "employeeRoleName")
    @Mapping(source = "rolePassword", target = "employeeRolePassword")
    @Mapping(source = "roleType", target = "employeeRoleType")
    @Mapping(source = "roleEmail", target = "employeeRoleEmail")
    @Mapping(source = "isDeleted", target = "employeeIsDeleted", qualifiedByName = "byteToBoolean")
    @Mapping(source = "createTime", target = "employeeCreateTime")
    @Mapping(source = "lastModifyTime", target = "employeeLastModifyTime")
    @Mapping(source = "version", target = "employeeVersion")
    EmployeeDto toDto(Employee employee);

    List<EmployeeDto> toDtoList(List<Employee> employees);

    @Mapping(source = "employeeName", target = "name")
    @Mapping(source = "employeePhone", target = "phone")
    @Mapping(source = "employeeSex", target = "sex")
    @Mapping(source = "employeeBirthday", target = "birthday")
    @Mapping(source = "employeeIntroduction", target = "introduce")
    @Mapping(source = "employeeAvatarUrl", target = "avatarUrl")
    @Mapping(source = "employeeNote", target = "note")
    @Mapping(source = "employeeRoleName", target = "roleName")
    @Mapping(source = "employeeRolePassword", target = "rolePassword")
    @Mapping(source = "employeeRoleType", target = "roleType")
    @Mapping(source = "employeeRoleEmail", target = "roleEmail")
    @Mapping(source = "employeeIsDeleted", target = "isDeleted", qualifiedByName = "booleanToByte")
    @Mapping(source = "employeeCreateTime", target = "createTime")
    @Mapping(source = "employeeLastModifyTime", target = "lastModifyTime")
    @Mapping(source = "employeeVersion", target = "version")
    Employee toEntity(EmployeeDto employeeDto);

    List<Employee> toEntityList(List<EmployeeDto> employeeDtoList);

    @Named("byteToBoolean")
    static Boolean byteToBoolean(Byte value) {
        return value != null && value != 0;
    }

    @Named("booleanToByte")
    static Byte booleanToByte(Boolean value) {
        return (byte) (value != null && value ? 1 : 0);
    }
}